<?php

Class admin_orderController Extends baseController {

	public function index() { 
		$where = '1=1 ';
		$having = '1=1';
		$trangthai_search = (isset($_REQUEST['trangthai'])) ? $_REQUEST['trangthai'] : '';
		$idhoadon_search = (isset($_REQUEST['hoadon_id'])) ? $_REQUEST['hoadon_id'] : '';
		$phuongthuc_search = (isset($_REQUEST['phuongthuc'])) ? $_REQUEST['phuongthuc'] : '';
		$giamin_search = (isset($_REQUEST['giamin'])) ? $_REQUEST['giamin'] : '';
		$giamax_search = (isset($_REQUEST['giamax'])) ? $_REQUEST['giamax'] : '';
		$ngayf_search = (isset($_REQUEST['ngayf'])) ? $_REQUEST['ngayf'] : '';
		$ngayt_search = (isset($_REQUEST['ngayt'])) ? $_REQUEST['ngayt'] : '';
		//echo str_replace('.','',$giamax_search);
		if($trangthai_search != '') $where .= ' and h.TrangThai = '.$trangthai_search;
		if($idhoadon_search != '') $where .= ' and h.id = '.$idhoadon_search;
		if($phuongthuc_search != '') $where .= ' and p.id = '.$phuongthuc_search;
		if($ngayf_search != '') $where .= ' and datediff(NgayDatHang,"'.date('Y-m-d',strtotime($ngayf_search)).'") >=0 '; 
		if($ngayt_search != '') $where .= ' and datediff(NgayDatHang,"'.date('Y-m-d',strtotime($ngayt_search)).'") <=0 ';
		if($giamin_search != '') $having .= ' and GiaHD >= '.str_replace('.','',$giamin_search);
		if($giamax_search != '') $having .= ' and GiaHD <= '.str_replace('.','',$giamax_search);
		
		$hoadonModel = new hoadon();
		$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
		$pos = ($current_page -1 ) * __PAGE_SIZE;
		$lsthd = $hoadonModel->getHoaDonByWherePaging($where,$having ,'h.id desc',$pos);
		$count = count((array)$hoadonModel->getHoaDonByWhere($where,$having,''));
		$pages = ceil($count/__PAGE_SIZE);

		$this->registry->template->lsthd = $lsthd; //echo '<pre>'; var_dump($lsthd);die;
		$this->registry->template->pages = $pages;
		$this->registry->template->show('admin/hoadon_view');
	}
	
	public function load_tivi() { 
		$tiviModel = new tivi();
		//echo 'aaaa';die;
		$current_page = (isset($_POST['cur'])) ? $_POST['cur'] : 1; 
		$pos = ($current_page -1 ) * __PAGE_SIZE; 
		$lstTv = $tiviModel->getTiviByWherePaging('t.TrangThai = 1 and t.SoLuong > 0','',$pos);
		$count = count((array)$tiviModel->getTiviByWhere('t.TrangThai = 1 and t.SoLuong > 0',''));
		$pages = ceil($count/__PAGE_SIZE);

		$this->registry->template->lstTivi = $lstTv;
		$this->registry->template->cur = $current_page;
		$this->registry->template->pages = $pages;
		$this->registry->template->show('admin/hoadon_loadtivi');
		die;
	}
	
	public function delete(){
		if(isset($_REQUEST['hoadon_id'])){
			$hoadonModel = new hoadon();
			$rs = $hoadonModel->deleteHoaDon($_REQUEST['hoadon_id']);
			if($rs){
				header('location:'.__URL_BASE.'index.php?rt=admin_order');
				exit;
			}
			else{
				echo '<script>alert("Có lỗi xảy ra.");</script>';
			}
		}
	}
	
	public function edit(){
		if(isset($_REQUEST['hoadon_id'])){
			$hoadon_id = $_REQUEST['hoadon_id'];
			$hoadonModel = new hoadon();
			$hoadon = $hoadonModel->getHoaDonById($hoadon_id);
			$this->registry->template->hoadon = $hoadon;
		}
		else{
			$phuongthucModel = new phuongthucthanhtoan();
			$lstPhuongthuc = $phuongthucModel->getPhuongThucThanhToanByWhere('TrangThai=1','');
			$this->registry->template->lstpt = $lstPhuongthuc;
		}
		$this->registry->template->show('admin/hoadon_edit');
	}
	
	//add or update order in admin panel
	public function save(){
		$name = $_POST['tname'];
		$email = $_POST['temail'];
		$phone = $_POST['tphone'];
		$address = $_POST['taddress'];
		$trangthai = $_POST['strangthai'];
		$hoadonModel = new hoadon();
		
		//update
		if(isset($_REQUEST['hoadon_id'])){
			$hoadon_id = $_REQUEST['hoadon_id'];
			$hoadon = $hoadonModel->getHoaDonById($hoadon_id);
			$lstchitiethd = $hoadonModel->getChiTietHoaDonByIdHoaDon($hoadon_id);
			$flag = false;
			foreach($lstchitiethd as $item){
				$count = 0;
				$tiviModel = new tivi();
				$tivi = $tiviModel->getTiviById($item->idTivi);
				// update tu chua xac nhan sang da xac nhan hoac da thanh toan -> tru so luong san pham tuong ung tu bang tblTivi
				if($hoadon->TrangThai == 0){
					if($trangthai == 1 || $trangthai == 2){ 
						$count = $tivi->SoLuong - $item->SoLuong;
						if($count >= 0){
							$lst_temp[$tivi->id] = $count;
							//$tiviModel->updateSoLuongById($tivi->SoLuong,$tivi->id);
						}
						else{
							$flag = true;
							break;
						}
					}
				}
				// update tu chua xac nhan sang da xac nhan hoac da thanh toan -> tru so luong san pham tuong ung tu bang tblTivi
				if($hoadon->TrangThai == 1 || $hoadon->TrangThai == 2){
					if($trangthai == 0){
						$count = $tivi->SoLuong + $item->SoLuong;
						$lst_temp[$tivi->id] = $count;
					}
				}
			}
			
			if($flag == false){
				//echo 'false'; die;
				$rs = $hoadonModel->updateHoaDon('TenKh="'.$name.'",EmailKH="'.$email.'",DienThoaiKH="'.$phone.'",DiaChiKH="'.$address.'",TrangThai='.$trangthai,$_REQUEST['hoadon_id']);
				if($rs){
					foreach($lst_temp as $k=>$v){
						$tiviModel->updateSoLuongById($v,$k);
					}
				}
			}
			else{
				
				header('location:'.__URL_BASE.'index.php?rt=admin_order/edit&hoadon_id='.$hoadon_id.'&count_err=true');
			}
			
			
		}
		
		//add
		else{
			//echo 'add'; die;
			$username = $_POST['stendangnhap'];
			$phuongthucid = $_POST['sphuongthuc'];
			$ngaydathang = date('Y-m-d');
			$lsttivi = $_POST['qty'];
			
			$hoadon->MaHoaDon = $hoadonModel->setMaHoaDon();
			$hoadon->TenDangNhap = $username;
			$hoadon->idPhuongThucThanhToan = $phuongthucid;
			$hoadon->NgayDatHang = $ngaydathang;
			$hoadon->TenKH = $name;
			$hoadon->EmailKH = $email;
			$hoadon->DienThoaiKH = $phone;
			$hoadon->DiaChiKH = $address;
			$hoadon->TrangThai = 0;
			
			$rs = $hoadonModel->insertHoaDon($hoadon);
			if($rs){
				$idhoadon = $hoadonModel->getMaxHoaDonId();
				foreach($lsttivi as $k=>$v){
					$idtivi = $k;
					$soluong = $v;
					$tiviModel = new tivi();
					$tivi = $tiviModel->getTiviById($idtivi);
					$gia = $tivi->GiaBan;
					$hoadonModel->insertChiTietHoaDon($idhoadon,$idtivi,$gia,$soluong);
					
				}
			}
		}
		if($rs){
			echo '<script>alert("thành công.");</script>';
			header('location:'.__URL_BASE.'index.php?rt=admin_order');
			exit;
		}
		else{
			echo '<script>alert("Có lỗi xảy ra.");</script>';
		}
	}

}

?>
